<?php
class Article_Data_Comments {
	
	private $form;
	private $database;
	private $secure;
	
	public function __construct(){
		$this->form = new Core_Driver_Firstform();
		$this->database = new Core_Driver_MysqlImproved();
		$this->secure = new Core_Data_Secure();
	}
	
	public function getComments(array $getVars){
		$getVars = $this->secure->parseData($getVars);
		$pageName = $getVars['page'];
		$comments['comments'] = $this->getCommentsIntoDatabase($pageName);
		$comments['form'] = $this->commentForm($pageName);
		return $comments;
	}
	
	public function addValues(array $postVars){
		$this->database->connect();
		$postVars = $this->secure->parseData($postVars);
		
		$nick = $postVars['nick'];
		$email = $postVars['email'];
		$webpage = $postVars['webpage'];
		$content = $postVars['content'];
		$pageName = $postVars['pageName'];
		$title = $postVars['title'];
		
		$nick = $this->database->escape($nick);
		$email = $this->database->escape($email);
		$webpage = $this->database->escape($webpage);
		$content = $this->database->escape($content);
		$pageName = $this->database->escape($pageName);
		$title = $this->database->escape($title);
		$this->database->prepare(
			"
				insert into page_comments(page_name, title, nickname, email, webpage, content) values('$pageName', '$title', '$nick', '$email', '$webpage', '$content');
				"
		);
		$this->database->query();
		$this->database->dataOperations();
 	}
	
	public function getCommentsCount($pageName){
		$this->database->connect();
		$this->database->prepare("select * from page_comments;");
		$this->database->query();
		$commentCount = 0;
		while($commentData = $this->database->fetch("array")){
			$commentCount++;
		}
		return $commentCount;
	}
	
	private function getCommentsIntoDatabase($pageName){
		$this->database->connect();
		$pageName = $this->database->escape($pageName);
		$this->database->prepare(
		"
				SELECT
				*
				FROM
				page_comments
				WHERE
				page_name = '$pageName'
				order
				by 
				id
				desc
				;
				"
		);
		$this->database->query();
		$comments = "";
		while($commentData = $this->database->fetch("array")){
			$comments .= "
					<h4>".$commentData['title']."</h4>
							<p><strong><a href=\"".$commentData['webpage']."\">".$commentData['nickname']."</a></strong> 
									<a href=\"mailto:".$commentData['email']."\">E-mail</a></p>
											<p>".$commentData['content']."</p>
					";
		}
		return $comments;
	}
	
	private function commentForm($pageName){
		$commentForm = '';
		$commentForm .= $this->form->startForm(
		array(
			"method" => "post",
				"action" => $_SERVER['REQUEST_URI']
		)
		); 
		$commentForm .= $this->form->inputForm(
			array(
			"type" => "hidden",
					"name" => "pageName",
					"value" => $pageName
		),
				null
		);
		$commentForm .= $this->form->labelForm(
			array(
			"for" => "nick"
		),
				"Enter your name: ",
				null
		);
		$commentForm .= $this->form->inputForm(
			array(
			"type" => "text", 
					"id" => "nick",
					"name" => "nick",
					"placeholder" => "Enter your nickname here ...",
					"maxlength" => "20"
		),
				"required autofocus"
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->labelForm(
			array(
			"for" => "email"
		),
				"Enter your e-mail address: ",
				null
		);
		$commentForm .= $this->form->inputForm(
			array(
			"type" => "email",
					"name" => "email",
					"id" => "email",
					"placeholder" => "Enter your email here ...",
					"maxlength" => "20"
		),
				"required autofocus"
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->labelForm(
				array(
						"for" => "title"
				),
				"Enter title: ",
				null
		);
		$commentForm .= $this->form->inputForm(
				array(
						"type" => "title",
						"name" => "title",
						"id" => "title",
						"placeholder" => "Enter your title here ...",
						"maxlength" => "20"
				),
				"required autofocus"
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->labelForm(
			array(
			"for" => "webpage"
		),
				"Enter your webpage address: ",
				null
		);
		$commentForm .= $this->form->inputForm(
			array(
			"type" => "url",
					"name" => "webpage",
					"id" => "webpage",
					"placeholder" => "Enter your webpage here ...",
					"maxlength" => "20"
		),
				"required autofocus"
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->labelForm(
			array(
			"for" => "content"
		),
				"Enter your content here:",
				null
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->textareaForm(
			array(
			"name" => "content",
					"maxlength" => "1000",
					"id" => "content",
					"placeholder" => "Enter your comment here ..."
		),
				"reuired",
				null
		);
		$commentForm .= "<br>";
		$commentForm .= $this->form->inputForm(
			array(
			"name" => "submit",
					"type" => "submit",
					"value" => "Add new comment"
		),
				null
		);
		$commentForm .= $this->form->endForm();
		return $commentForm;
	}
	
}